import { GameData } from "../game/GameData";
import { GameState } from "../game/GameState";
import { TaskMgr } from "../game/TaskMgr";
cc.Class({
    extends: require('BasePanel'),

    properties: {
        labContent: cc.Label,
    },
    onLoad() {
        this._super();
    },

    onEnter(args, action = true) {
        this._super(args, action);
        this._updateLabel();

        if (G.config.addCoin) {
            G.ad.show(G.ad.AD_BANNER);
        }
    },

    onExit(args) {
        this._super(args);
        G.ad.hide(G.ad.AD_BANNER);
    },

    _updateLabel() {
        let animalIndex = GameState.getMaxIndexCanBuyByCoin();
        let price = GameState.getAnimalCoinPrice(animalIndex);
        price = G.bignumber.plus(price, 2);
        this.labContent.string = `观看视频即可获得${G.bignumber.toUnitType(price)}金币`;
    },

    show() {
        this._super();

    },

    hide(cb) {
        this._super(cb);
    },
    onEnable() {

    },
    onDisable() {
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnClose') {
            this.onClose();
        } else if (name == 'btnShare') {
            G.partner.sendEvent('金币', { '类型': '金币' });
            G.ad.show(G.ad.AD_VIDEO, (param) => {
                if (param.event == G.ad.EVENT_SHOW_SUCC) {
                    G.partner.sendEvent('视频广告', { '类型': '获取金币-成功' });
                    let animalIndex = GameState.getMaxIndexCanBuyByCoin();
                    let price = GameState.getAnimalCoinPrice(animalIndex);
                    price = G.bignumber.mult(price, 2);
                    GameData.addCoin(price);

                    G.game.toast('金币+' + G.bignumber.toUnitType(price), 1);
                    TaskMgr.addDaliyTask(5, 1);
                    this._onClose();
                } else {
                    G.partner.sendEvent('分享', { '类型': '金币' });
                    G.partner.share({ title: '给我俩萌宠，合出全世界' }, () => {
                        this.onClose();
                        let animalIndex = GameState.getMaxIndexCanBuyByCoin();
                        let price = GameState.getAnimalCoinPrice(animalIndex);
                        price = G.bignumber.mult(price, 2);
                        GameData.addCoin(price);

                        G.game.toast('金币+' + G.bignumber.toUnitType(price), 1);
                        TaskMgr.addDaliyTask(5, 1);
                    });
                }
            })

        }
    },

    onTouchStart() {

    },
});
